<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        body {
            background: #ddd;
        }

        #canvas {
            margin: 10px;
            background: #fff;
            border: thin inset #aaa;
        }
    </style>
    <script>
        onload = function () {
            function windowToCanvas(canvas, clientX, clientY) {
                var boundingRect = canvas.getBoundingClientRect();
//                console.log(boundingRect);
                //canvas画布的boundingRect边界矩形有六个属性
                //height: 322 canvas画布的高
                //width: 622 canvas画布的宽
                //top: 30 基于整个window的顶边的位置
                //bottom: 352 基于整个window的底边的位置
                //left: 10 基于整个window的左边的位置
                //right: 632 基于整个window的右边的位置


                //P19 书上绝壁有错误
                return {
                    //鼠标的x-boundingRect的x
                    x: (clientX - boundingRect.left) / (boundingRect.width / canvas.width), //boundingRect.width/canvas.width就是放大的倍数
                    y: (clientY - boundingRect.top) / (boundingRect.height / canvas.height)
                }

            }

            var canvas = document.getElementById('canvas');
            var context = canvas.getContext('2d');//d必须小写

            //锚点 就是第一和第四个点
            var anchorPoints = [{x: 130, y: 70}, {x: 430, y: 270}];
            //控制点
            var controlPoints= [{x: 130, y: 250}, {x: 450, y: 70}];

            //画网格
            function drawSolidGrid(step) {
                context.save();
                context.strokeStyle = 'lightgray';
                context.lineWidth = 0.5;
                for (var i = step + 0.5; i < canvas.width; i += step) {
                    context.beginPath();
                    context.moveTo(i, 0);
                    context.lineTo(i, canvas.height);
                    context.stroke();
                }
                for (var i = step + 0.5; i < canvas.height; i += step) {
                    context.beginPath();
                    context.moveTo(0, i);
                    context.lineTo(canvas.width, i);
                    context.stroke();
                }
                context.restore();
            }
            drawSolidGrid(10);

            function drawBezierCurve(){
                context.strokeStyle='blue';
                context.beginPath();
                context.moveTo(anchorPoints[0].x,anchorPoints[0].y);
                context.bezierCurveTo(controlPoints[0].x,controlPoints[0].y,controlPoints[1].x,controlPoints[1].y,anchorPoints[1].x,anchorPoints[1].y);
                context.stroke();
            }

            drawBezierCurve();
        }
    </script>
</head>
<body>
<canvas id='canvas' width='600' height='300'>
    Canvas not supported!
</canvas>
</body>
</html>